package cn.lcfms.app.admin.service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.lcfms.app.admin.bean.ArticleBean;
import cn.lcfms.app.admin.bean.CategoryBean;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;
import cn.lcfms.utils.StringUtils;
@Service
public class DataService {
	@Transactional
	public boolean saveArticle(ArticleBean article){
		boolean b=true;
		try {
			BaseService service = Application.getService("articles");	
			if(article.getId()==0) {
				service.insert(article);
			}else {
				HashMap<String, Object> map=new HashMap<>();
				map.put("id", article.getId());
				map.put("title", article.getTitle());
				map.put("category_id", article.getCategory_id());
				map.put("labels", StringUtils.join(article.getLabels(), ","));		
				map.put("desc", article.getDesc());
				map.put("details", article.getDetails());
				map.put("mid",article.getMid());
				service.where("id=#{id}");
				service.setData(map);
				service.update("title","category_id","labels","desc","details","mid");
			}
			int id = article.getId();
			JSONArray uploads = JSONArray.fromObject(article.getImgfile());
			if(id!=0 && uploads.size()>0){	
				for(int i=0;i<uploads.size();i++){
					JSONObject object = uploads.getJSONObject(i);						
					int fid=(int) object.get("id");
					service.setTable("files");
					service.where("id="+fid);
					service.setData(id,"articles");
					service.update("file_type_id","file_type");
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return b;
	}	
	
	@Transactional
	public boolean saveCategory(CategoryBean cate){
		boolean b=true;
		try {	
			BaseService service=Application.getService("categories");	
			if(cate.getId()!=0) {
				HashMap<String, Object> map=new HashMap<>();
				map.put("id", cate.getId());
				map.put("title", cate.getTitle());
				map.put("pid", cate.getPid());
				map.put("mid",cate.getMid());
				map.put("labels", StringUtils.join(cate.getLabels(), ","));
				map.put("desc", cate.getDesc());
				service.where("id=#{id}");
				service.setData(map);
				service.update("title","pid","labels","mid","desc");			
			}else {
				service.insert(cate);
			}		
			int id = cate.getId();
			JSONArray uploads = JSONArray.fromObject(cate.getImgfile());
			if(id!=0 && uploads.size()>0){	
				for(int i=0;i<uploads.size();i++){
					JSONObject object = uploads.getJSONObject(i);						
					int fid=(int) object.get("id");
					service.setTable("files");
					service.where("id",fid);
					service.setData(id,"categories");
					service.update("file_type_id","file_type");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			b=false;
		}
		return b;
	}
	
	/**
	 * 根据id递归获取栏目
	 * @param id
	 * @return
	 */
	public HashMap<Integer, String> getCategory(int id){
		HashMap<Integer, String> map=new HashMap<>();
		getCategory(id,map);
		return map;
	}
	
	private void getCategory(int id,HashMap<Integer, String> map) {
		BaseService service=Application.getService("categories");
		service.where("id", id);
		HashMap<String, Object> r = service.selectMap();
		if(!r.containsKey("id")) {
			return;
		}
		int pid=(int) r.get("pid");
		String title=(String) r.get("title");
		map.put(id, title);
		if(pid!=0) {
			getCategory(pid,map);
		}
	}
	
	public String[] getModel() {
		BaseService service=Application.getService("model");
		service.column("mid","name");
		List<HashMap<String, Object>> list = service.selectList();
		StringBuffer text=new StringBuffer();
		StringBuffer value=new StringBuffer();
		for(int i=0;i<list.size();i++) {
			HashMap<String, Object> map=list.get(i);
			text.append(map.get("name"));
			value.append(map.get("mid"));
			if(i!=list.size()-1) {
				text.append(",");
				value.append(",");
			}
		}
		String[] r= {text.toString(),value.toString()};
		return r;
	}
	
	public JSONArray getCategoryFile(int cateId) {
		BaseService service= Application.getService("files");
		service.where("file_type", "categories");
		service.where("file_type_id", cateId);
		List<HashMap<String, Object>> list = service.selectList();
		JSONArray array=new JSONArray();
		for(int i=0;i<list.size();i++) {
			JSONObject object=new JSONObject();
			String type=(String) list.get(i).get("type");
			String original=(String) list.get(i).get("original");			
			object.put("type", type);
			object.put("id", list.get(i).get("id"));
			String path="";
			String[] split = original.split("/");			
			if(type.equals("img")) {
				object.put("url", Application.APP_PATH+"uploadfile/images/"+original);	
				path= Application.ROOT_PATH+"uploadfile"+File.separator+"images"+File.separator+split[0]+File.separator+split[1];
			}else {
				object.put("url", Application.APP_PATH+"uploadfile/file/"+original);
				path= Application.ROOT_PATH+"uploadfile"+File.separator+"file"+File.separator+split[0]+File.separator+split[1];
			}			
			File file=new File(path);
			object.put("size", file.length());
			array.add(object);
		}
		return array;
	}
	
	public JSONArray getArticleFile(int cateId) {
		BaseService service= Application.getService("files");
		service.where("file_type", "articles");
		service.where("file_type_id", cateId);
		List<HashMap<String, Object>> list = service.selectList();
		JSONArray array=new JSONArray();
		for(int i=0;i<list.size();i++) {
			JSONObject object=new JSONObject();
			String type=(String) list.get(i).get("type");
			String original=(String) list.get(i).get("original");			
			object.put("type", type);
			object.put("id", list.get(i).get("id"));
			String path="";
			String[] split = original.split("/");			
			if(type.equals("img")) {
				object.put("url", Application.APP_PATH+"uploadfile/images/"+original);	
				path= Application.ROOT_PATH+"uploadfile"+File.separator+"images"+File.separator+split[0]+File.separator+split[1];
			}else {
				object.put("url", Application.APP_PATH+"uploadfile/file/"+original);
				path= Application.ROOT_PATH+"uploadfile"+File.separator+"file"+File.separator+split[0]+File.separator+split[1];
			}			
			File file=new File(path);
			object.put("size", file.length());
			array.add(object);
		}
		return array;
	}
}
